﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="AssistanceAssignment.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GenericTools;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using DataModels.Enumerations;
using DataModels.Common;

namespace ReportingManagement
{
    public partial class MainWindow
    {
        private bool contactsModified = false;
        private bool tabPageAssistanceAssignmentLoaded = false;

        private void tabPageAssistanceAssignment_Enter(object sender, EventArgs e)
        {
            if (!tabPageAssistanceAssignmentLoaded)
            {
                try
                {
                    PopulateRegisteredUser();
                    try
                    {
                        int userID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());
                        FillRegisteredUserDetails(userID);
                    }
                    catch { }

                    Cursor.Current = Cursors.WaitCursor;
                    toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
                    DataTable districtDataTable = null;
                    if (this.authenticatedUser.Level == AccessLevel.ADMINISTRATOR)
                    {
                        districtDataTable = Utilities.GetAllDistricts();
                    }
                    else
                    {
                        districtDataTable = Utilities.GetAllDistricts(this.authenticatedUser.SupportingPartnerId);
                    }
                    Cursor.Current = Cursors.Default;
                    toolStripStatusLabel.Text = "Ready";

                    AvailableListBox.Items.Clear();
                    AssignedListBox.Items.Clear();
                    PopulateListBox(districtDataTable);
                    PopulateAssigned();

                    UpdateSaveDetailsButton();
                    DistrictRadioButton_CheckedChanged(null, null);
                    tabPageAssistanceAssignmentLoaded = true;
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex);
                }
            }
        }

        private void PopulateRegisteredUser()
        {
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetRegisteredUsers();
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            RegisteredUserComboBox.DisplayMember = "Name";
            RegisteredUserComboBox.ValueMember = "ApprovedCellNo";
            RegisteredUserComboBox.DataSource = dataTable;
        }

        private void FillRegisteredUserDetails(int userID)
        {
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable userContacts = Utilities.GetRegisteredUserContacts(userID);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            if (userContacts.Rows.Count > 0)
            {
                CellNumberTextBox.Text = userContacts.Rows[0][0].ToString();
                EmailTextBox.Text = userContacts.Rows[0][1].ToString();
            }
            else
            {
                CellNumberTextBox.Text = null;
                EmailTextBox.Text = null;
            }
            this.contactsModified = false;
        }

        private void RegisteredUserComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int userID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());
                FillRegisteredUserDetails(userID);
                HandleListBoxes();
            }
            catch (Exception ex)
            {
                if (tabPageAssistanceAssignmentLoaded) DisplayErrorMessage(ex);
            }
        }

        private void BtnSaveUserDetails_Click(object sender, EventArgs e)
        {
            if ((AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DATA_ACCESS) ||
                (AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DIRECT_ACCESS))
            {
                string message = @"The application is running in offline mode. " +
                    "You can not perform any activities that require server " +
                    "connection while you are offline. " +
                    "\n\nYou are current operation will not be processed!";
                MessageBox.Show(this, message, AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            int userID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());
            string cellnum = CellNumberTextBox.Text;
            string email = EmailTextBox.Text;
            bool success = false;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
                success = Utilities.SaveRegisteredUserContacts(userID, cellnum, email);
                Cursor.Current = Cursors.Default;
                toolStripStatusLabel.Text = "Ready";
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex);
                success = false;
            }
            BtnSaveUserDetails.Enabled = !success;
        }

        private void UpdateSaveDetailsButton()
        {
            if (String.IsNullOrEmpty(CellNumberTextBox.Text) && String.IsNullOrEmpty(EmailTextBox.Text))
            {
                BtnSaveUserDetails.Enabled = false;
            }
            else BtnSaveUserDetails.Enabled = true;
            this.contactsModified = true;
        }

        private void CellNumberTextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateSaveDetailsButton();
        }

        private void EmailTextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateSaveDetailsButton();
        }

        private void PopulateListBox(DataTable dataTable)
        {
            AvailableListBox.SuspendLayout();
            foreach (DataRow row in dataTable.Rows)
            {
                ListItem item = new ListItem();
                item.Value = row[0];
                item.Text = row[1].ToString(); ;
                AvailableListBox.Items.Add(item);
            }
            AvailableListBox.ResumeLayout();
            PopulateAssigned();
            UpdateMovementButtons();
        }

        private void FacilityRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (FacilityRadioButton.Checked)
            {
                AvailabeLabel.Text = "Available Facilities";
                AssignedLabel.Text = "Assigned Facilities";
                RemoveCheckBox.Text = "Remove Already Assigned Facilities";
                try
                {
                    HandleListBoxes();
                    lstAreas.Items.Clear();
                    lblArea.Text = "Facilities with assistance providers";
                    DataTable dataTable = Utilities.GetAssignedFacilities();
                    foreach (DataRow row in dataTable.Rows)
                    {
                        string code = row["FacCode"].ToString();
                        string name = row["FacName"].ToString();
                        ListItem item = new ListItem();
                        item.Text = name;
                        item.Value = code;
                        lstAreas.Items.Add(item);
                    }
                    if (lstAreas.Items.Count > 0) lstAreas.SelectedIndex = 0;
                    else
                    {
                        lstAreas.SelectedIndex = -1;
                        lstAssignedUsers.Items.Clear();
                        lblAssignedUsers.Text = "Assigned Users";
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex);
                }
            }
        }

        private void DistrictRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (DistrictRadioButton.Checked)
            {
                AvailabeLabel.Text = "Available Districts";
                AssignedLabel.Text = "Assigned Districts";
                RemoveCheckBox.Text = "Remove Already Assigned Districts";
                try
                {
                    HandleListBoxes();
                    lstAreas.Items.Clear();
                    lblArea.Text = "Districts with assistance providers";
                    DataTable dataTable = Utilities.GetAssignedDistricts();
                    foreach (DataRow row in dataTable.Rows)
                    {
                        int id = Int32.Parse(row["DistrictID"].ToString());
                        string name = row["District"].ToString();
                        ListItem item = new ListItem();
                        item.Text = name;
                        item.Value = id;
                        lstAreas.Items.Add(item);
                    }
                    if (lstAreas.Items.Count > 0) lstAreas.SelectedIndex = 0;
                    else
                    {
                        lstAreas.SelectedIndex = -1;
                        lstAssignedUsers.Items.Clear();
                        lblAssignedUsers.Text = "Assigned Users";
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex);
                }
            }
        }

        private void RegionRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (RegionRadioButton.Checked)
            {
                AvailabeLabel.Text = "Available Regions";
                AssignedLabel.Text = "Assigned Regions";
                RemoveCheckBox.Text = "Remove Already Assigned Regions";
                try
                {
                    HandleListBoxes();
                    lstAreas.Items.Clear();
                    lblArea.Text = "Regions with assistance providers";
                    DataTable dataTable = Utilities.GetAssignedRegions();
                    foreach (DataRow row in dataTable.Rows)
                    {
                        int id = Int32.Parse(row["RegionID"].ToString());
                        string name = row["RegionName"].ToString();
                        ListItem item = new ListItem();
                        item.Text = name;
                        item.Value = id;
                        lstAreas.Items.Add(item);
                    }
                    if (lstAreas.Items.Count > 0) lstAreas.SelectedIndex = 0;
                    else
                    {
                        lstAreas.SelectedIndex = -1;
                        lstAssignedUsers.Items.Clear();
                        lblAssignedUsers.Text = "Assigned Users";
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex);
                }
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            if (AvailableListBox.SelectedIndex == -1) AvailableListBox.SelectedIndex = 0;
            ListItem selectedItem = (ListItem)AvailableListBox.SelectedItem;
            AddListItem2(AvailableListBox, AssignedListBox);
            UpdateMovementButtons();
        }

        private void BtnRemove_Click(object sender, EventArgs e)
        {
            if (AssignedListBox.SelectedIndex == -1) AssignedListBox.SelectedIndex = 0;
            ListItem selectedItem = (ListItem)AssignedListBox.SelectedItem;
            AddListItem2(AssignedListBox, AvailableListBox);
            UpdateMovementButtons();
        }

        private void AddListItem2(ListBox source, ListBox destination)
        {
            ListItem selectedItem = (ListItem)source.SelectedItem;
            int selectedIndex = source.SelectedIndex;
            source.Items.RemoveAt(selectedIndex);

            if (source.Items.Count > 0)
            {
                if (selectedIndex != 0) selectedIndex--;
                source.SelectedIndex = selectedIndex;
            }

            destination.Items.Add(selectedItem);
            destination.SelectedIndex = destination.Items.Count - 1;
        }

        private void UpdateMovementButtons()
        {
            int assigned = AssignedListBox.Items.Count;
            int available = AvailableListBox.Items.Count;

            BtnAdd.Enabled = (available > 0);
            BtnRemove.Enabled = (assigned > 0);
        }

        private void AvailableListBox_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (AvailableListBox.SelectedIndex == -1) AvailableListBox.SelectedIndex = 0;
                ListItem selectedItem = (ListItem)AvailableListBox.SelectedItem;
                AddListItem2(AvailableListBox, AssignedListBox);
                UpdateMovementButtons();
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex);
            }
        }

        private void AssignedListBox_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (AssignedListBox.SelectedIndex == -1) AssignedListBox.SelectedIndex = 0;
                ListItem selectedItem = (ListItem)AssignedListBox.SelectedItem;
                AddListItem2(AssignedListBox, AvailableListBox);
                UpdateMovementButtons();
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex);
            }
        }

        private void BtnSaveAssignment_Click(object sender, EventArgs e)
        {
            if ((AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DATA_ACCESS) ||
                (AppConfiguration.AccessMethod == DataAccessMethod.OFFLINE_DIRECT_ACCESS))
            {
                string message = @"The application is running in offline mode. " +
                    "You can not perform any activities that require server " +
                    "connection while you are offline. " +
                    "\n\nYou are current operation will not be processed!";
                MessageBox.Show(this, message, AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            BtnSaveAssignment.Enabled = false;
            if (String.IsNullOrEmpty(CellNumberTextBox.Text) || String.IsNullOrEmpty(EmailTextBox.Text))
            {
                MessageBox.Show(this, "Please provide valid cellphone number and e-mail address",
                    "Error - Incomplete fields", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                if (String.IsNullOrEmpty(CellNumberTextBox.Text)) CellNumberTextBox.Focus();
                else EmailTextBox.Focus();
                BtnSaveAssignment.Enabled = true;
                return;
            }
            else
            {
                try
                {
                    if (contactsModified)
                    {
                        int userID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());
                        string cellnum = CellNumberTextBox.Text;
                        string email = EmailTextBox.Text;

                        Cursor.Current = Cursors.WaitCursor;
                        toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
                        Utilities.SaveRegisteredUserContacts(userID, cellnum, email);
                        Cursor.Current = Cursors.Default;
                        toolStripStatusLabel.Text = "Ready";
                    }
                    AssignToUser();
                    MessageBox.Show(this, "You have successfully make an assistance assignment",
                        "Assistance Assignment", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BtnSaveAssignment.Enabled = true;
                    return;
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex);
                }
            }
        }

        private void AssignToUser()
        {
            int ID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());
            List<string> assignmentIDs = new List<string>();
            foreach (ListItem i in AssignedListBox.Items)
            {
                string id = i.Value.ToString();
                assignmentIDs.Add(id);
            }
            ReportLevel level = ReportLevel.NATION;
            if (RegionRadioButton.Checked) level = ReportLevel.REGION;
            if (DistrictRadioButton.Checked) level = ReportLevel.DISTRICT;
            if (FacilityRadioButton.Checked) level = ReportLevel.FACILITY;

            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            Utilities.AssignToUser(level, ID, assignmentIDs);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";
        }

        #region Getting Assistance Assignments
        private DataTable GetSelectedUserAssignedRegions()
        {
            int userID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());

            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetSelectedUserAssignedRegions(userID);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }

        private DataTable GetAssignedRegions(int userToExclude)
        {
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetAssignedRegions(userToExclude);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }

        private DataTable GetSelectedUserAssignedDistricts()
        {
            int userID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());

            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetSelectedUserAssignedDistricts(userID);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }

        private DataTable GetAssignedDistricts()
        {
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetAssignedDistricts();
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }

        private DataTable GetAssignedDistricts(int userToExclude)
        {
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetAssignedDistricts(userToExclude);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }

        private DataTable GetSelectedUserAssignedFacilities()
        {
            int userID = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());

            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetSelectedUserAssignedFacilities(userID);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }

        private DataTable GetAssignedFacilities()
        {
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetAssignedFacilities();
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }

        private DataTable GetAssignedFacilities(int userToExclude)
        {
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            DataTable dataTable = Utilities.GetAssignedFacilities(userToExclude);
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            return dataTable;
        }
        #endregion

        private void PopulateAssigned()
        {
            DataTable assignedDataTable = null;
            try
            {
                if (RegionRadioButton.Checked) assignedDataTable = GetSelectedUserAssignedRegions();
                if (DistrictRadioButton.Checked) assignedDataTable = GetSelectedUserAssignedDistricts();
                if (FacilityRadioButton.Checked) assignedDataTable = GetSelectedUserAssignedFacilities();
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex);
                return;
            }

            foreach (DataRow row in assignedDataTable.Rows)
            {
                if (FacilityRadioButton.Checked)
                {
                    string facilityCode = row[0].ToString();
                    foreach (ListItem item in AvailableListBox.Items)
                    {
                        string itemCode = item.Value.ToString();
                        if (itemCode == facilityCode)
                        {
                            AvailableListBox.Items.Remove(item);
                            AssignedListBox.Items.Add(item);
                            break;
                        }
                    }
                }
                else
                {
                    int ID = int.Parse(row[0].ToString());
                    foreach (ListItem item in AvailableListBox.Items)
                    {
                        int itemID = int.Parse(item.Value.ToString());
                        if (itemID == ID)
                        {
                            AvailableListBox.Items.Remove(item);
                            AssignedListBox.Items.Add(item);
                            break;
                        }
                    }
                }
            }
            UpdateMovementButtons();
        }

        private void RemoveCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (RemoveCheckBox.Checked) HandleRemoveAlreadyAssigned();
                else
                {
                    if (RegionRadioButton.Checked) RegionRadioButton_CheckedChanged(null, null);
                    if (DistrictRadioButton.Checked) DistrictRadioButton_CheckedChanged(null, null);
                    if (FacilityRadioButton.Checked) FacilityRadioButton_CheckedChanged(null, null);
                }
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex);
            }
        }

        private void HandleRemoveAlreadyAssigned()
        {
            DataTable assignedDataTable = null;
            int userToExclude = int.Parse(RegisteredUserComboBox.SelectedValue.ToString());
            if (RegionRadioButton.Checked) assignedDataTable = GetAssignedRegions(userToExclude);
            if (DistrictRadioButton.Checked) assignedDataTable = GetAssignedDistricts(userToExclude);
            if (FacilityRadioButton.Checked) assignedDataTable = GetAssignedFacilities(userToExclude);

            if (RemoveCheckBox.Checked)
            {
                foreach (DataRow row in assignedDataTable.Rows)
                {
                    if (FacilityRadioButton.Checked)
                    {
                        string assigned = row[0].ToString();
                        // Remove assigned facilities in available list
                        foreach (ListItem i in AvailableListBox.Items)
                        {
                            string availableCode = i.Value.ToString();
                            if (availableCode == assigned)
                            {
                                AvailableListBox.Items.Remove(i);
                                break;
                            }
                        }
                        // Remove selected assigned facilities in selected list
                        foreach (ListItem i in AssignedListBox.Items)
                        {
                            string assignedCode = i.Value.ToString();
                            if (assignedCode == assigned)
                            {
                                AssignedListBox.Items.Remove(i);
                                break;
                            }
                        }
                    }

                    // Dealing with regions and districts
                    else
                    {
                        int assigned = int.Parse(row[0].ToString());
                        // Remove assigned districts or regions in the available list
                        foreach (ListItem i in AvailableListBox.Items)
                        {
                            int availableID = int.Parse(i.Value.ToString());
                            if (availableID == assigned)
                            {
                                AvailableListBox.Items.Remove(i);
                                break;
                            }
                        }
                        // Remove selected assigned districts or regions in the selected list
                        foreach (ListItem i in AssignedListBox.Items)
                        {
                            int assignedID = int.Parse(i.Value.ToString());
                            if (assignedID == assigned)
                            {
                                AssignedListBox.Items.Remove(i);
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                //// Adding assigned facilities, districts or regions into available regions
                //foreach (DataRow row in assignedDataTable.Rows)
                //{
                //    ListItem item = new ListItem();
                //    if (FacilityRadioButton.Checked) item.Value = row[0].ToString();
                //    else item.Value = int.Parse(row[0].ToString());
                //    item.Text = row[1].ToString();
                //    AvailableListBox.Items.Add(item);
                //}
            }
            UpdateMovementButtons();
        }

        private void HandleListBoxes()
        {
            DataTable dataTable = null;
            Cursor.Current = Cursors.WaitCursor;
            toolStripStatusLabel.Text = "Loading data, please wait..."; Application.DoEvents();
            if (RegionRadioButton.Checked) dataTable = Utilities.GetAllRegions();
            if (DistrictRadioButton.Checked) dataTable = Utilities.GetAllDistricts();
            //if (FacilityRadioButton.Checked) dataTable = Utilities.GetAllFacilities();
            if (FacilityRadioButton.Checked) dataTable = Utilities.GetAllFacilitiesStatus();
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel.Text = "Ready";

            AvailableListBox.Items.Clear();
            AssignedListBox.Items.Clear();
            PopulateListBox(dataTable);
            HandleRemoveAlreadyAssigned();

            //if (RemoveCheckBox.Checked) HandleRemoveAlreadyAssigned();
        }

        private void lstAreas_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dataTable = null;
                ListItem item = (ListItem)lstAreas.SelectedItem;
                lstAssignedUsers.Items.Clear();

                Cursor.Current = Cursors.WaitCursor;
                toolStripStatusLabel.Text = "Loading data, please wait...";
                Application.DoEvents();

                if (FacilityRadioButton.Checked)
                {
                    string facilityCode = item.Value.ToString();
                    dataTable = Utilities.GetFacilityAssignedUsers(facilityCode);
                }

                if (DistrictRadioButton.Checked)
                {
                    int districtID = Int32.Parse(item.Value.ToString());
                    dataTable = Utilities.GetDistrictAssignedUsers(districtID);
                }

                if (RegionRadioButton.Checked)
                {
                    int regionID = Int32.Parse(item.Value.ToString());
                    dataTable = Utilities.GetRegionAssignedUsers(regionID);
                }

                if (dataTable != null)
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        string[] data = new string[] 
                        { 
                            row[0].ToString(), 
                            row[1].ToString()
                        };
                        ListViewItem user = new ListViewItem(data, -1);
                        lstAssignedUsers.Items.Add(user);
                    }
                }
                if (lstAreas.Items.Count > 0) lblAssignedUsers.Text = String.Format("{0}: Assigned Users", item.Text);
                else lblAssignedUsers.Text = "Assigned Users";

                toolStripStatusLabel.Text = "Ready";
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex);
            }
        }

        private void btnRefreshAssistanceAssignment_Click(object sender, EventArgs e)
        {
            tabPageAssistanceAssignmentLoaded = false;
            tabPageAssistanceAssignment_Enter(sender, e);
        }
    }
}
